/******************************************************************************
 * Copyright (C) 2015  671643387@qq.com
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *****************************************************************************/

#ifndef GAME_SERVER_OPCODEHANDLER_H_
#define GAME_SERVER_OPCODEHANDLER_H_

#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif

#include <cstdint>
#include <boost/unordered_map.hpp>
#include "Shared/Binary/WorldPacket.h"


enum SessionStatus
{
    STATUS_AUTHED = 0,                     ///< Player authenticated (_player==NULL, m_playerRecentlyLogout = false or will be reset before handler call)
    STATUS_LOGGEDIN,                       ///< Player in game (_player!=NULL, inWorld())
    STATUS_TRANSFER,                       ///< Player transferring to another map (_player!=NULL, !inWorld())
    STATUS_LOGGEDIN_OR_RECENTLY_LOGGEDOUT, ///< _player!= NULL or _player==NULL && m_playerRecentlyLogout)
    STATUS_NEVER,                          ///< Opcode not accepted from client (deprecated or server side only)
    STATUS_UNHANDLED                       ///< We don' handle this opcode yet
};

enum PacketProcessing
{
    PROCESS_INPLACE = 0,   ///< process packet whenever we receive it - mostly for non-handled or non-implemented packets
    PROCESS_THREADUNSAFE,  ///< packet is not thread-safe - process it in \ref World::UpdateSessions
    PROCESS_THREADSAFE     ///< packet is thread-safe - process it in \ref Map::Update
};

class GameSession;

struct OpcodeHandler
{
    ///A string representation of the name of this opcode (see \ref Opcodes)
    char const* name;
    ///The status for this handler, it tells whether or not we will handle the packet at all and
    ///when we will handle it.
    SessionStatus status;
    ///This tells where the packet should be processed, ie: is it thread un/safe, which in turn
    ///determines where it will be processed
    PacketProcessing packetProcessing;
    ///The callback called for this opcode which will work some magic
    void (GameSession::*handler)(Shared::Binary::WorldPacket& packet);
};

typedef boost::unordered_map<std::uint16_t, OpcodeHandler>	OpHandlerMap;

class Opcodes
{
public:
    Opcodes(void);
    ~Opcodes(void);

    void buildOpcodeList();
    void storeOpcode(::std::uint16_t Opcode, char const* name, SessionStatus status, 
        PacketProcessing process, void (GameSession::*handler)(Shared::Binary::WorldPacket& recvPacket))
    {
        OpcodeHandler& ref = _opHandlers[Opcode];
        ref.name = name;
        ref.status = status;
        ref.packetProcessing = process;
        ref.handler = handler;
    }

    /// Lookup opcode
    OpcodeHandler const* lookupOpcode(::std::uint16_t id) const
    {
        OpHandlerMap::const_iterator itr = _opHandlers.find(id);
        if (itr != _opHandlers.end())
        {
            return &itr->second;
        }
        return NULL;
    }

    OpcodeHandler const& operator[](::std::uint16_t id) const
    {
        OpHandlerMap::const_iterator itr = _opHandlers.find(id);
        if (itr != _opHandlers.end())
        {
            return itr->second;
        }
        return _emptyHandler;
    }

    static Opcodes *instance(void);
private:
    OpHandlerMap _opHandlers;
    static Opcodes* _instance;
    const static OpcodeHandler _emptyHandler;
};

#define gameOpcodes	(*Opcodes::instance())

inline const char* lookupOpcodeName(::std::uint16_t id)
{
    if (OpcodeHandler const* op = gameOpcodes.lookupOpcode(id))
    {
        return op->name;
    }
    return "received unknown opcode, it's more than max!";
}

#endif // GAME_SERVER_OPCODEHANDLER_H_
